// 图形层
import 'package:flutter/material.dart';
import 'package:flutter_app/constant/const_string.dart';
import 'package:flutter_app/pages/white_board/shape_type.dart';
import 'package:flutter_app/pages/white_board/white_board_vm.dart';
import 'package:flutter_app/utils/paint_util.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';

class GraphicsLayerWidget extends StatelessWidget {
  ShapeType currentShape;
  GraphicsLayerWidget(this.currentShape, {super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<WhiteBoardVM>(
      id: ConstString.graphicsLayerWidgetId,
      builder: (WhiteBoardVM controller) {
        return RepaintBoundary(
          child: Transform(
              alignment: Alignment.center,
              transform: Matrix4.identity()
                ..translate(controller.curCanvasOffset.dx,
                    controller.curCanvasOffset.dy)
                ..scale(controller.curCanvasScale),
              child: CustomPaint(
                isComplex: true,
                painter: GraphicsPainter(currentShape),
              )),
        );
      },
    );
  }
}

class GraphicsPainter extends CustomPainter {
  ShapeType currentShape;
  GraphicsPainter(this.currentShape);

  final WhiteBoardVM _whiteBoardVM = Get.find<WhiteBoardVM>();

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = const Color.fromARGB(255, 9, 159, 182)
      ..strokeWidth = 2;
    for (var graphics in _whiteBoardVM.graphicsList) {
      switch (graphics.shapeType) {
        case ShapeType.line:
          break;
        case ShapeType.rectangle:
          break;
        case ShapeType.path:
          paint.style = PaintingStyle.stroke;
          paint.color = graphics.color;
          canvas.drawPath(Path()..addPolygon(graphics.points, true), paint);
          if (graphics.isSelected) {
            PaintUtil.drawBoundingRectAndControlPoint(
                canvas, graphics.points, paint);
          }
          break;
        case ShapeType.rect:
        case ShapeType.circle:
        // TODO: Handle this case.
        case ShapeType.triangle:
        // TODO: Handle this case.
        case ShapeType.text:
        // TODO: Handle this case.
        case ShapeType.image:
        // TODO: Handle this case.
        case ShapeType.eraser:
        // TODO: Handle this case.
        case ShapeType.none:
          // TODO: Handle this case.
          break;
        default:
      }
    }
    // switch (currentShape) {
    //   case ShapeType.line:
    //     canvas.drawLine(Offset(100, 100), Offset(200, 200), paint);
    //     break;
    //   case ShapeType.rectangle:
    //     canvas.drawRect(
    //         Rect.fromCenter(center: Offset(100, 100), width: 100, height: 100),
    //         paint);
    //   case ShapeType.path:
    //     canvas.save();
    //     paint.color = const Color.fromARGB(255, 38, 179, 13);
    //     final pathPoints = [
    //       const Offset(100, 100),
    //       const Offset(150, 230),
    //       const Offset(200, 200),
    //       const Offset(290, 100),
    //     ];
    //     // 绘制三角形
    //     paint.style = PaintingStyle.fill;
    //     canvas.drawPath(Path()..addPolygon(pathPoints, true), paint);

    //     // 绘制边界
    //     paint.color = const Color.fromARGB(255, 11, 178, 234);
    //     paint.style = PaintingStyle.stroke;
    //     final boundRect = PaintUtil.getBoundData(pathPoints);
    //     canvas.drawRect(boundRect.$3, paint);

    //     // 绘制控制点
    //     canvas.restore();
    //     final controlPointPaint = Paint()
    //       ..color = const Color.fromARGB(255, 43, 26, 227);
    //     const controlPointSize = 10.0;
    //     for (var point in boundRect.$1) {
    //       PaintUtil.drawSquare(
    //           canvas, point, controlPointSize, controlPointPaint);
    //     }
    //   default:
    // }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
